export const getNodeInfo = (data, key, callback) => {
  data.forEach((item, index, arr) => {
    if (item.standardName === key) {
      return callback(item, index, arr);
    }
    if (item.children) {
      return getNodeInfo(item.children, key, callback);
    }
  });
};

const nodeData = { isEdit: true, editLabel: "" };
export const updateChildrenById = (treeData, tagNodeData, nodeKey = "id") => {
  treeData.some(node => {
    if (node[nodeKey] && node[nodeKey] === tagNodeData[nodeKey]) {
      if (node.children?.length) {
        node.children.unshift({ ...nodeData, format: tagNodeData.format, parentId: tagNodeData.templateId });
      } else {
        node.children = [{ ...nodeData, format: tagNodeData.format, parentId: tagNodeData.templateId }];
      }
      return true;
    } else {
      updateChildrenById(node?.children || [], tagNodeData, nodeKey);
    }
  });
  return treeData;
};

export const deleteNode = (treeData, nodeId, nodeKey = "id") => {
  treeData.some((item, i) => {
    if (item[nodeKey] === nodeId) {
      treeData.splice(i, 1);
      return true;
    }
    if (item?.children?.length) {
      return item.children.some((subItem, index) => {
        if (subItem[nodeKey] === nodeId) {
          item.children.splice(index, 1);
          return true;
        } else {
          deleteNode(subItem?.children || [], nodeId, nodeKey);
        }
      });
    }
  });
};

export const initFormData = {
  standardName: "",
  parentId: 0,
  showOrder: "",
  standardDesc: "",
  shortcomingDesc: "",
  picIds: "",
  picUrls: "",
  videoIds: "",
  videoUrls: ""
};
